﻿#include "axisplotdatawidget.h"

QPlotDataWidget::QPlotDataWidget(int i32AixNo,QString sAxisName,QWidget *parent) : QWidget(parent)
{
    m_color[0]=QColor(0,153,78);
    m_color[1]=QColor(163,217,0);
    m_color[2]=QColor(240,0,61);
    m_color[3]=QColor(253,220,0);
    m_color[4]=QColor(128,0,128);
    m_color[5]=QColor(0,181,244);
    m_color[6]=QColor(255,102,0);
    m_color[7]=QColor(128,128,0);
    m_color[8]=QColor(219,0,194);
    m_color[9]=QColor(0,128,128);
    m_color[10]=QColor(0,0,255);
    m_color[11]=QColor(200,204,0);
    m_color[12]=QColor(14,114,204);
    m_color[13]=QColor(245,147,17);
    m_color[14]=QColor(250,67,67);
    m_color[15]=QColor(223,56,129);
    m_color[16]=QColor(56,203,125);
    m_color[17]=QColor(221,174,51);
    m_color[18]=QColor(178,53,230);
    m_color[19]=QColor(95,102,148);
    m_color[20]=QColor(149,162,255);
    m_color[21]=QColor(250,128,128);

    _i32CurrentX = 0;
    _i32AxisNo = i32AixNo;
    _sAxisName = sAxisName;

    InitUI();
}

QPlotDataWidget::~QPlotDataWidget()
{


}

/**
* @brief
* @note
*
*/
void QPlotDataWidget::InitUI()
{
    _pCustomPlot = new QCustomPlot(this);
//    _pCustomPlot->setBackground(QColor(54,55,57));
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, QColor(120, 120, 120));
    axisRectGradient.setColorAt(1, QColor(80, 80, 80));
    _pCustomPlot->axisRect()->setBackground(axisRectGradient);   // 设置QCPAxisRect背景颜色

    _pCustomPlot->xAxis->setLabel("时间");
    _pCustomPlot->xAxis->setLabelColor(QColor(80, 80, 80));
    _pCustomPlot->xAxis->setTickLabelColor(QColor(80, 80, 80));
    QPen pen;
    pen.setColor(Qt::gray);//主刻度红色
    _pCustomPlot->xAxis->setTickPen(pen);
    _pCustomPlot->xAxis->setSubTickPen(pen);

    _pCustomPlot->yAxis->setLabel("数据");
    _pCustomPlot->yAxis->setLabelColor(QColor(80, 80, 80));
    _pCustomPlot->yAxis->setTickLabelColor(QColor(80, 80, 80));
    _pCustomPlot->yAxis->setTickPen(pen);
    _pCustomPlot->yAxis->setSubTickPen(pen);

    _pCustomPlot->xAxis->setRange(0, 1024);//设置范围
    _pCustomPlot->yAxis->setRange(0, 100);

    _pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect);

    _pCustomPlot->legend->setVisible(true);
    _pCustomPlot->legend->setFont(QFont("Helvetica",9));


    QStringList sDataNameList;
    sDataNameList<<"位置"<<"速度"<<"Q相电流";
    QHBoxLayout* hplayout = new QHBoxLayout();
    QLabel* pLabel = new QLabel("选择:");
    pLabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");
    hplayout->addWidget(pLabel);

    for(int i = 0; i < sDataNameList.size();i++)
    {
        _pCustomPlot->addGraph();//增加通道

        QPen pen;
        pen.setColor(m_color[i]);
        pen.setWidth(1);
        _pCustomPlot->graph(i)->setPen(pen);
        _pCustomPlot->graph(i)->setName(sDataNameList[i]);

        QPushButton* pButton = new QPushButton(this);
        setButtonStyleSheet(pButton);
        pButton->setCheckable(true);
        pButton->setText(sDataNameList[i]);
        pButton->setObjectName(QString::number(i));
        hplayout->addWidget(pButton);



        connect(pButton,&QPushButton::clicked,this,&QPlotDataWidget::onSelectDataBtnClick);
    }
    hplayout->addItem(new QSpacerItem(300,1,QSizePolicy::Expanding,QSizePolicy::Fixed));

    QVBoxLayout* vlayout = new QVBoxLayout(this);
    vlayout->addLayout(hplayout,1);
    vlayout->addWidget(_pCustomPlot,5);
}

/**
* @brief
* @note
*
*/
void QPlotDataWidget::onSelectDataBtnClick(bool bchecked)
{
    QPushButton* pbtn =  (QPushButton*)(this->sender());
    int nIndex = pbtn->objectName().toInt();

    if(bchecked == true)
    {
        _selectdatalist.append(nIndex);
        _xdatalist[nIndex].clear();
        _ydatalist[nIndex].clear();
    }
    else
    {
        for(int i = 0; i < _selectdatalist.size();i++)
        {
            if(_selectdatalist[i] == nIndex)
            {
                _selectdatalist.removeAt(i);
                break;
            }
        }
    }
}

/**
* @brief
* @note
*
*/
void QPlotDataWidget::onRcvData(QVariant var)
{
    _rcv_capdata = var.value<st_rcvdata>();

    //画图显示
    if(_selectdatalist.size() == 0)
    {
        return;
    }

    _i32CurrentX++;


    for(int i = 0; i < _selectdatalist.size();i++)
    {
        int ndataIndex = _selectdatalist[i];

        double xdata = _i32CurrentX*2;//ms
        _xdatalist[ndataIndex].append(xdata);

        if(ndataIndex == 0x00 && _i32AxisNo == M_AXIS_AZI)//位置
        {
            fourchar l_data;
            l_data.data[3] = 0;
            l_data.data[2] = _rcv_capdata.azimuthresponse_value[0];
            l_data.data[1] = _rcv_capdata.azimuthresponse_value[1];
            l_data.data[0] = _rcv_capdata.azimuthresponse_value[2];
            double l_value = l_data.ui32data*360.0/16777216;
            _ydatalist[ndataIndex].append(l_value);//数据
        }

        if(ndataIndex == 0x01&& _i32AxisNo == M_AXIS_AZI)//速度
        {
            if(_rcv_capdata.loopcmd == 0x11)
            {
                twochar l_data;
                l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_speed_response_value.data[1];
                l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_speed_response_value.data[0];
                float lfdata = l_data.i16data /100.0;
                _ydatalist[ndataIndex].append(lfdata);
            }
        }

        if(ndataIndex == 0x02&& _i32AxisNo == M_AXIS_AZI)//Q相电流
        {
            if(_rcv_capdata.loopcmd == 0x11)
            {
                twochar l_data;
                l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_currentQ_response_value.data[1];
                l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_currentQ_response_value.data[0];
                float lfdata = l_data.i16data /1000.0;
                _ydatalist[ndataIndex].append(lfdata);
            }
        }

        if(ndataIndex == 0x00 && _i32AxisNo == M_AXIS_PICTH)//位置
        {
            fourchar l_data;
            l_data.data[3] = 0;
            l_data.data[2] = _rcv_capdata.pitchresponse_value[0];
            l_data.data[1] = _rcv_capdata.pitchresponse_value[1];
            l_data.data[0] = _rcv_capdata.pitchresponse_value[2];

            double l_value = l_data.ui32data*360.0/16777216;
            _ydatalist[ndataIndex].append(l_value);//数据
        }

        if(ndataIndex == 0x01&& _i32AxisNo == M_AXIS_PICTH)//速度
        {
            if(_rcv_capdata.loopcmd == 0x11)
            {
                twochar l_data;
                l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_speed_response_value.data[1];
                l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_speed_response_value.data[0];
                float lfdata = l_data.i16data /100.0;
                _ydatalist[ndataIndex].append(lfdata);
            }
        }

        if(ndataIndex == 0x02&& _i32AxisNo == M_AXIS_PICTH)//电流
        {
            if(_rcv_capdata.loopcmd == 0x11)
            {
                twochar l_data;
                l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_currentQ_response_value.data[1];
                l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_currentQ_response_value.data[0];
                float lfdata = l_data.i16data /1000.0;
                _ydatalist[ndataIndex].append(lfdata);
            }
        }

        _pCustomPlot->graph(ndataIndex)->setData(_xdatalist[ndataIndex],_ydatalist[ndataIndex]);
    }
    double xdata = _i32CurrentX*2;//ms
//    float lfMin = xdata -1000 > 0?xdata -1000:0;
//    _pCustomPlot->xAxis->setRange(lfMin, xdata);//设置范围  可视范围为1000个数

    if(_i32CurrentX%100 == 0)
    {
        for(int i = 0; i < _selectdatalist.size();i++)
        {
            int ndataIndex = _selectdatalist[i];
            _pCustomPlot->graph(ndataIndex)->setData(_xdatalist[ndataIndex],_ydatalist[ndataIndex]);
        }

        _pCustomPlot->xAxis->setRange(0, xdata+10);//设置范围  可视范围为1000个数
        _pCustomPlot->replot();
    }

}

void QPlotDataWidget::setButtonStyleSheet(QPushButton* pBtn)
{

    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "font-size:18px;font-family: 'Microsoft YaHei';}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"

                                );

    pBtn->setMinimumHeight(30);
    pBtn->setMinimumWidth(80);

}
